const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const cors = require('cors')
app.listen(3000, () => {
  console.log('服务器已开启')
});

//post接参
app.use(express.urlencoded({extended: false}))
app.use(express.json())

// 引入 db
const myquery = require('../js/db');

//配置cors
app.use(cors())

//资源静态化
app.use(express.static(__dirname));

//登录
app.post('/login',async (req, res,next) => {
  let {
    username,
    password
  } = req.body
  if (!username || !password) {
    next("用户名或密码不能为空")
    return
  }
  let sql = `select * from member where username = '${username}' and password = '${password}'`;
  let [err, result] = await myquery(sql);
  if (err) {
    next(err);
    return;
  }
  if (result.length > 0) {
    res.send({code: 200,msg:'登录成功'});
    return;
  }
    next('用户名或密码错误')
})

app.use((err, req, res, next) => {
  res.send({
    code: 400,
    msg: err
  })
})
